package com.youlu.campus.web.course;

import com.alibaba.fastjson.JSON;
import com.youlu.campus.entity.course.dto.*;
import com.youlu.campus.entity.course.group.Info;
import com.youlu.campus.entity.course.group.VideoStudy;
import com.youlu.campus.entity.course.vo.CourseVideoVo;
import com.youlu.campus.service.course.LiteHomeService;
import com.youlu.campus.web.common.base.RequestContext;
import com.youlu.campus.web.common.utils.CommonUtils;
import com.yuelin.infrastructure.quantum.common.QResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 小程序首页信息
 * @author : [dengbo]
 * @className : LiteHomeController
 * @description : [小程序首页信息]
 * @createTime : [2022/5/17 17:42]
 */
@Slf4j
@RestController
//@RequestMapping("/mini-app/")
public class LiteHomeController {

    @Value("${sys.pay.wx.payWechatNotifyUrl}")
    private String notifyUrl;

    @Resource
    private LiteHomeService liteHomeService;

    /**
     * 视频课程详情接口
     * 参数：request - 课程信息查询请求类
     */
    @PostMapping("/mini-app/course/info")
    public QResult courseInfo(@Validated(Info.class) @RequestBody CourseInfoDTO request) {
        String userId = RequestContext.getUserId();
        log.info("LiteHomeController,courseInfo,userId:{},request param:{}", userId, JSON.toJSONString(request));
        request.setUserId(userId);
        request.setAppId(RequestContext.getAppId());
        request.setPlatform(RequestContext.getCurPlatform());
        request.setUserPO(RequestContext.getCurUser());
        return QResult.success(liteHomeService.getCourseInfo(request));
    }

    /**
     * 课程学习
     * 参数：request - 课程学习请求类
     */
    @PostMapping("/mini-app/course/video/study")
    public QResult videoStudy(@Validated(VideoStudy.class) @RequestBody CourseInfoDTO request) {
        String userId = RequestContext.getUserId();
        log.info("LiteHomeController,videoStudy,userId:{},request param:{}", userId, JSON.toJSONString(request));
        request.setUserId(userId);
        request.setAppId(RequestContext.getAppId());
        request.setPlatform(RequestContext.getCurPlatform());
        request.setUserPO(RequestContext.getCurUser());
        boolean data = liteHomeService.courseVideoStudy(request);
        log.info("LiteHomeController,videoStudy,response:{}", data);
        return QResult.success(data);
    }

    /**
     * 视频课程中视频信息和学习状态查询接口
     * 参数：request - 视频详情
     */
    @PostMapping("/mini-app/course/video/detail")
    public QResult videoDetail(@Validated @RequestBody UserVideoDTO request) {
        String userId = RequestContext.getUserId();
        log.info("videoDetail,userId:{},request param:{}", userId, request);
        request.setUserId(userId);
        request.setAppId(RequestContext.getAppId());
        request.setPlatform(RequestContext.getCurPlatform());
        request.setUserPO(RequestContext.getCurUser());
        CourseVideoVo data = liteHomeService.getVideoDetail(request);
        log.info("videoDetail,response:{}", data);
        return QResult.success(data);
    }

    /**
     * 视频课程支付接口
     * 参数：request - 课程支付请求类
     */
    @PostMapping("/mini-app/course/pay")
    public QResult coursePay(@Validated @RequestBody CourseInfoDTO request, HttpServletRequest httpRequest) {
        String userId = RequestContext.getUserId();
        request.setUserId(userId);
        request.setAppId(RequestContext.getAppId());
        request.setPlatform(RequestContext.getCurPlatform());
        request.setUserPO(RequestContext.getCurUser());
        request.setClientIp(CommonUtils.getRemoteIp(httpRequest));
        request.setNotifyUrl(notifyUrl);
        log.info("LiteHomeController,coursePay,userId:{},request param:{}", userId, JSON.toJSONString(request));
        Object data = liteHomeService.createOrder(request);
        log.info("LiteHomeController,coursePay,response:{}", data);
        return QResult.success(data);
    }

    /**
     * 用户报名活动列表
     * 参数：request - 用户报名活动列表
     */
    @PostMapping("/mini-app/user/register/activity/page/info")
    public QResult getUserRegisterActivityList(@Validated @RequestBody RegisterActivityDTO request) {
        String userId = RequestContext.getUserId();
        request.setUserId(userId);
        request.setAppId(RequestContext.getAppId());
        request.setPlatform(RequestContext.getCurPlatform());
        request.setUserPO(RequestContext.getCurUser());
        request.setUserIdList(RequestContext.getCurUserIdList());
        log.info("LiteHomeController,getUserRegisterActivityList,userId:{},request param:{}", userId, JSON.toJSONString(request));
        return QResult.success(liteHomeService.getUserRegisterActivityList(request));
    }

    /**
     * 获取第三方视频课程地址
     * 参数：request - 获取三方视频地址
     */
    @PostMapping("/mini-app/get/third/course/url")
    public QResult getThirdCourseUrl(@Validated @RequestBody ThirdCourseDTO request) {
        String userId = RequestContext.getUserId();
        request.setUserId(userId);
        request.setAppId(RequestContext.getAppId());
        request.setPlatform(RequestContext.getCurPlatform());
        request.setUserPO(RequestContext.getCurUser());
        log.info("LiteHomeController,getThirdCourseUrl,userId:{},request param:{}", userId, JSON.toJSONString(request));
        return QResult.success(liteHomeService.getThirdCourseUrl(request));
    }

    /**
     * 获取kos 活动详情
     * 参数：request - 活动详情
     */
    @PostMapping("/mini-app/get/kos/activity/detail")
    public QResult getKosActivityDetail(@Validated @RequestBody KosActivityDTO request) {
        log.info("LiteHomeController,getKosActivityDetail,request param:{}", request);
        String userId = RequestContext.getUserId();
        request.setUserId(userId);
        request.setAppId(RequestContext.getAppId());
        request.setPlatform(RequestContext.getCurPlatform());
        request.setUserPO(RequestContext.getCurUser());
        return QResult.success(liteHomeService.getKosActivityDetail(request));
    }

    /**
     * kos 活动订单支付
     * 参数：request - 活动详情
     */
    @PostMapping("/mini-app/kos/order/pay")
    public QResult createKosOrder(@Validated @RequestBody KosActivityDTO request, HttpServletRequest httpRequest) {
        log.info("LiteHomeController,getKosActivityDetail,request param:{}", request);
        String userId = RequestContext.getUserId();
        request.setUserId(userId);
        request.setAppId(RequestContext.getAppId());
        request.setPlatform(RequestContext.getCurPlatform());
        request.setUserPO(RequestContext.getCurUser());
        request.setClientIp(CommonUtils.getRemoteIp(httpRequest));
        request.setNotifyUrl(notifyUrl);
        return QResult.success(liteHomeService.createKosOrder(request));
    }

    /**
     * 米粒商城登录(没有用了)
     * 参数：request - 米粒登录
     */
    @PostMapping("/mini-app/mili/login")
    public QResult miliLogin() {
        return QResult.success(liteHomeService.miliLogin(RequestContext.getCurUser()));
    }

    /**
     * 微信小程序菜单(目前没有用了)
     * 参数：request - 小程序菜单控制
     */
    @PostMapping("/mini-app/wechat/little/menu")
    public QResult getWechatMenu() {
        return QResult.success(liteHomeService.getWechatMenu(RequestContext.getCurUser()));
    }

    /**
     * 更新用户头像
     * 参数：request - 更新用户头像
     */
    @PostMapping("/mini-app/update/user/logo")
    public QResult updateUserLogo(@Validated @RequestBody UserLogoSaveDTO request) {
        request.setUserId(RequestContext.getUserId());
        request.setOpenId(RequestContext.getCurUser().getOpenId());
        liteHomeService.updateUserInfo(request);
        return QResult.success(true);
    }

}